package com.example.concurrency.volatileDemo;

public class VolatileDemo
{


    public static void main(String asg[])
    {
        MyData myData = new MyData();
        new Thread(() ->
        {
            try
            {
                Thread.sleep(2000);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            myData.setNumber(1);
            System.out.println("number: " + myData.getNumber());
        }).start();

        while (myData.getNumber() == 0)
        {
//            System.out.println("main number: ");
            //不加入睡眠会一直读不到最新值
            //但是打印后 会刷新最新值?
        }

        System.out.println("----------------- number" + myData.getNumber());
    }
}

class MyData
{
    private int number = 0;

    public void setNumber(int number)
    {
        this.number = number;
    }

    public int getNumber()
    {
        return number;
    }
}
